package com.nit.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nit.server.common.QueryPageParam;
import com.nit.server.common.RentApplyParam;
import com.nit.server.common.Result;
import com.nit.server.common.UseApplyParam;
import com.nit.server.entity.*;
import com.nit.server.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *

 */
@RestController
@RequestMapping("/rentapply")
public class RentapplyController {
    @Autowired
    private RentapplyService rentapplyService;
    @Autowired
    private RentgoodsService rentgoodsService;
    @Autowired
    private RentrecordService rentrecordService;
    //新增
    @PostMapping("/add")
    public Result add(@RequestBody Rentapply rentapply){
        return rentapplyService.save(rentapply)?Result.success():Result.fail();
    }
    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String rentapplyId){
        return rentapplyService.removeById(rentapplyId)?Result.success():Result.fail();
    }
    @PostMapping("/listPage")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result listPage(@RequestBody QueryPageParam pageParam){
        Page<Rentapply> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());
        String cId = (String)pageParam.getParams().get("cId");
        String userId = (String)pageParam.getParams().get("userId");
        QueryWrapper<Rentapply> queryWrapper = new QueryWrapper();
        queryWrapper.apply("a.user_id = c.user_id and a.rentgoods_id = b.rentgoods_id");
        if (StringUtils.isNotBlank(cId)){
            queryWrapper.eq("b.c_id",cId);
        }
        if (StringUtils.isNotBlank(userId)){
            queryWrapper.eq("a.user_id",userId);
        }
        IPage result = rentapplyService.pageCC(page,queryWrapper);

        return Result.success(result.getTotal(), result.getRecords());
    }
    //通过
    @PostMapping("/checkyes")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result checkyes(@RequestBody RentApplyParam param){
        System.out.println(param);
        int userID = param.getUserId();
        int rentapplyCount = param.getRentapplyCount();
        int adminId = param.getAdminId();
        String rentapplyId = String.valueOf(param.getRentapplyId());
        String rentgoodsId = String.valueOf(param.getRentgoodsId());
        Rentgoods rentgoods = rentgoodsService.getById(rentgoodsId);
        int newnum = rentgoods.getRentgoodsCount()-rentapplyCount;
        rentgoods.setRentgoodsCount(newnum);
        rentgoodsService.updateById(rentgoods);

        Rentrecord rentrecord = new Rentrecord();
        rentrecord.setRentgoodsId(Integer.valueOf(rentgoodsId));
        rentrecord.setRentapplyCount(rentapplyCount);
        rentrecord.setUserId(userID);
        rentrecord.setAdminId(adminId);
        rentrecord.setRentrecordStatus(1);
        rentrecord.setRentrecordReturn(2);
        rentrecordService.save(rentrecord);
        return rentapplyService.removeById(rentapplyId)?Result.success():Result.fail();
    }
    //未通过
    @PostMapping("/checkno")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result checkno(@RequestBody RentApplyParam param){
        int userID = param.getUserId();
        int rentapplyCount = param.getRentapplyCount();
        String rentapplyId = String.valueOf(param.getRentapplyId());
        String rentgoodsId = String.valueOf(param.getRentgoodsId());
        int adminId = param.getAdminId();
        Rentrecord rentrecord = new Rentrecord();
        rentrecord.setRentgoodsId(Integer.valueOf(rentgoodsId));
        rentrecord.setRentapplyCount(rentapplyCount);
        rentrecord.setUserId(userID);
        rentrecord.setAdminId(adminId);
        rentrecord.setRentrecordStatus(2);
        rentrecord.setRentrecordReturn(0);
        rentrecordService.save(rentrecord);
        return rentapplyService.removeById(rentapplyId)?Result.success():Result.fail();
    }
}
